home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 60 / IOPROG_60.ISO / soft / c++ / gsl-1.1.1-setup.exe / {app} / src / siman / test.c < prev   
Encoding:
C/C++ Source or Header  |  2002-04-18  |  4.6 KB  |  162 lines

  1. /* siman/test.c
  2.  * 
  3.  * Copyright (C) 1996, 1997, 1998, 1999, 2000 Mark Galassi
  4.  * 
  5.  * This program is free software; you can redistribute it and/or modify
  6.  * it under the terms of the GNU General Public License as published by
  7.  * the Free Software Foundation; either version 2 of the License, or (at
  8.  * your option) any later version.
  9.  * 
  10.  * This program is distributed in the hope that it will be useful, but
  11.  * WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13.  * General Public License for more details.
  14.  * 
  15.  * You should have received a copy of the GNU General Public License
  16.  * along with this program; if not, write to the Free Software
  17.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  */
  19.  
  20. #include <config.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <math.h>
  24. #include <gsl/gsl_test.h>
  25. #include <gsl/gsl_rng.h>
  26. #include <gsl/gsl_siman.h>
  27. #include <gsl/gsl_ieee_utils.h>
  28. #include <stdio.h>
  29.  
  30. /* set up parameters for this simulated annealing run */
  31. #define N_TRIES 200        /* how many points do we try before stepping */
  32. #define ITERS_FIXED_T 1000    /* how many iterations for each T? */
  33. #define STEP_SIZE 1.0        /* max step size in random walk */
  34. #define K 1.0            /* Boltzmann constant */
  35. #define T_INITIAL 0.008        /* initial temperature */
  36. #define MU_T 1.003        /* damping factor for temperature */
  37. #define T_MIN 2.0e-6
  38.  
  39. gsl_siman_params_t params = {N_TRIES, ITERS_FIXED_T, STEP_SIZE,
  40.                  K, T_INITIAL, MU_T, T_MIN};
  41.  
  42. inline double square (double x) ;
  43. inline double square (double x) { return x * x ; } 
  44.  
  45. double E1(void *xp);
  46. double M1(void *xp, void *yp);
  47. void S1(const gsl_rng * r, void *xp, double step_size);
  48. void P1(void *xp);
  49.  
  50. /* now some functions to test in one dimension */
  51. double E1(void *xp)
  52. {
  53.   double x = * ((double *) xp);
  54.  
  55.   return exp(-square(x-1))*sin(8*x) - exp(-square(x-1000))*0.89;
  56. }
  57.  
  58. double M1(void *xp, void *yp)
  59. {
  60.   double x = *((double *) xp);
  61.   double y = *((double *) yp);
  62.  
  63.   return fabs(x - y);
  64. }
  65.  
  66. void S1(const gsl_rng * r, void *xp, double step_size)
  67. {
  68.   double old_x = *((double *) xp);
  69.   double new_x;
  70.  
  71.   new_x = gsl_rng_uniform(r)*2*step_size - step_size + old_x;
  72.  
  73.   memcpy(xp, &new_x, sizeof(new_x));
  74. }
  75.  
  76. void P1(void *xp)
  77. {
  78.   printf(" %12g ", *((double *) xp));
  79. }
  80.  
  81. int main(void)
  82. {
  83.   double x_min = 1.36312999455315182 ;
  84.   double x ;
  85.  
  86.   gsl_rng * r = gsl_rng_alloc (gsl_rng_env_setup()) ;
  87.  
  88.   gsl_ieee_env_setup ();
  89.  
  90.   /* The function tested here has multiple mimima. 
  91.      The global minimum is at    x = 1.36312999, (f = -0.87287)
  92.      There is a local minimum at x = 0.60146196, (f = -0.84893) */
  93.  
  94.   x = -10.0 ;
  95.   gsl_siman_solve(r, &x, E1, S1, M1, NULL, NULL, NULL, NULL,
  96.           sizeof(double), params);
  97.   gsl_test_rel(x, x_min, 1e-3, "f(x)= exp(-(x-1)^2) sin(8x), x0=-10") ;
  98.  
  99.   x = +10.0 ;
  100.   gsl_siman_solve(r, &x, E1, S1, M1, NULL, NULL, NULL, NULL,
  101.           sizeof(double), params);
  102.   gsl_test_rel(x, x_min, 1e-3, "f(x)= exp(-(x-1)^2) sin(8x), x0=10") ;
  103.  
  104.   /* Start at the false minimum */
  105.  
  106.   x = +0.6 ; 
  107.   gsl_siman_solve(r, &x, E1, S1, M1, NULL, NULL, NULL, NULL,
  108.           sizeof(double), params);
  109.   gsl_test_rel(x, x_min, 1e-3, "f(x)= exp(-(x-1)^2) sin(8x), x0=0.6") ;
  110.  
  111.   x = +0.5 ; 
  112.   gsl_siman_solve(r, &x, E1, S1, M1, NULL, NULL, NULL, NULL,
  113.           sizeof(double), params);
  114.   gsl_test_rel(x, x_min, 1e-3, "f(x)= exp(-(x-1)^2) sin(8x), x0=0.5") ;
  115.  
  116.   x = +0.4 ; 
  117.   gsl_siman_solve(r, &x, E1, S1, M1, NULL, NULL, NULL, NULL,
  118.           sizeof(double), params);
  119.   gsl_test_rel(x, x_min, 1e-3, "f(x)= exp(-(x-1)^2) sin(8x), x0=0.4") ;
  120.  
  121.   exit (gsl_test_summary ());
  122.  
  123. #ifdef JUNK 
  124.   x0.D1 = 12.0;
  125.   printf("#one dimensional problem, x0 = %f\n", x0.D1);
  126.   gsl_siman_Usolve(r, &x0, test_E_1D, test_step_1D, distance_1D,
  127.            print_pos_1D, params);
  128.  
  129.  
  130.   x0.D2[0] = 12.0;
  131.   x0.D2[1] = 5.5;
  132.   printf("#two dimensional problem, (x0,y0) = (%f,%f)\n",
  133.      x0.D2[0], x0.D2[1]);
  134.   gsl_siman_Usolve(r, &x0, test_E_2D, test_step_2D, distance_2D,
  135.            print_pos_2D, params); 
  136.  
  137.   x0.D3[0] = 12.2;
  138.   x0.D3[1] = 5.5;
  139.   x0.D3[2] = -15.5;
  140.   printf("#three dimensional problem, (x0,y0,z0) = (%f,%f,%f)\n",
  141.      x0.D3[0], x0.D3[1], x0.D3[2]);
  142.   gsl_siman_Usolve(r, &x0, test_E_3D, test_step_3D, distance_3D, 
  143.            print_pos_3D, params); 
  144.  
  145.   x0.D2[0] = 12.2;
  146.   x0.D2[1] = 5.5;
  147.  
  148.   gsl_siman_solve(r, &x0, test_E_2D, test_step_2D, distance_2D, print_pos_2D, params);
  149.   
  150.   x0.D3[0] = 12.2;
  151.   x0.D3[1] = 5.5;
  152.   x0.D3[2] = -15.5;
  153.  
  154.   gsl_siman_solve(r, &x0, test_E_3D, test_step_3D, distance_3D, print_pos_3D, params);
  155.  
  156.   return 0;
  157. #endif
  158. }
  159.  
  160.  
  161.  
  162.